#!/usr/bin/python
"""
/* generator.py - sudoku puzzle generator
 *
 * Copyright 2009
 * Authored by Erin Yueh <erinyueh@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
"""

import os, sys, time, getopt
import random
from sudoku import solver

GROUP_ROWCOL = {
                '0': (0,0,0), '1':(1,0,3), '2':(2,0,6),
                '3': (3,3,0), '4':(4,3,3), '5':(5,3,6),
                '6': (6,6,0), '7':(7,6,3), '8':(8,6,6),
               }
class SudokuGenerator:
 def __init__(self):
        print ''
        self.level = 3
 def generate_single(self,level):
        puzzle = []
        for i in xrange(9):
            row = [0,0,0,0,0,0,0,0,0]
            puzzle.append(row)
        self.generate_dummygroup(puzzle)
        verify = solver.SudokuSolver()
        if (verify.solve(puzzle)):
            pass
        if(level>0):
            self.remove_elements(puzzle,int(level))
        else:
            self.remove_elements(puzzle,int(self.level))
        line = self.arrangetoLine(puzzle)
        return puzzle
 def generate_dummygroup(self,puzzle):
    # generate 3 dummy group
    for g in (0,4,8):
        items = [1,2,3,4,5,6,7,8,9]
        random.shuffle(items)
        (group,r,c) = GROUP_ROWCOL[str(g)]
        for i in xrange (r,r+3):
            for j in xrange(c,c+3):
                value = items.pop()
                puzzle[i][j] = value

    return True

 def remove_elements(self,puzzle,level):

    for i in xrange(9):
        remove = random.sample([0,1,2,3,4,5,6,7,8],int(level))
        for j in remove:
            puzzle[i][j] = 0
    return True
    

 def arrangetoLine(self,puzzle):
    line = ""
    for i in xrange(9):
        for j in xrange(9):
            line += str(puzzle[i][j])

    return line
    
def exit_with_usage(self):
    print globals()['__doc__']
    os._exit(1)
    
def main():
    
    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'h:f:n:l:', ['help','h','?'])
	
    except Exception, e:
        print str(e)
        exit_with_usage()

    options = dict(optlist)
    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
       print "Help:"
       exit_with_usage()

    generator = SudokuGenerator()
    (number,level) = (1000,3)
    if '-n' in options:
       number = options["-n"]
    if '-l' in options:
        level = options["-l"]

    output = 'puzzle_' + str(number) + '_' +str(level)
    f = open(output, 'w') 
    for x in xrange(int(number)):
        puzzle = []
        for i in xrange(9):
            row = [0,0,0,0,0,0,0,0,0]
            puzzle.append(row)
        generator.generate_dummygroup(puzzle)
        verify = solver.SudokuSolver()
        if (verify.solve(puzzle)):
            pass
        generator.remove_elements(puzzle,int(level))
        line = generator.arrangetoLine(puzzle)
        f.write(line)
        f.write("\n")
    f.close()

if __name__ == "__main__":
	main()
